<template>
  <view class="container">
		<nav-ber :title="check? '调研任务' : '新建调研任务-患者'" :bgColor="'#f7f8ff'"></nav-ber>
		<view class="province">
			<uFrom ref="getFrom" :style="{'pointer-events': !looks? 'none': 'auto'}" :itemList="itemList" :itemKeys.sync="itemKeys" @setdata="setdataFun"></uFrom>
			<view class="">
				<view style="margin: 20rpx 0;">调研资料</view>
				<view class="researc_flex">
					<view class="button_bgcolor">
						问卷
					</view>
					<view style="color: lightskyblue;" @click="quertionFun">
						{{ examinationName }}
					</view>
					<view class="button_bgcolor" style="background-color: cornflowerblue;">
						{{ (check || answerList.length > 0)? '已完成' : '未完成' }} 
					</view>
				</view>
			</view>
			<view v-if="looks" class="button_str" @click="confirm">
				保存并提交
			</view>
		</view>
		<u-modal v-if="showTimefilter" :show="showTimefilter" @cancel="showTimefilter = false" @confirm="confirmFunXz" showCancelButton :content="contentText"></u-modal>
  </view>
</template>
<script>
	import { itemList } from './items.js'
	import store from '@/store'
	import { timed_task_type } from '@/api/task.js'
	import { getApplyById, setTaskResearch, researcBuId, staskResearch, listpaper, hospitalById, verifyFunDy  } from '@/api/mytaskManagement.js'
  export default {
    data() {
			return {
				datasAll: {},
				showTimefilter: false,
				contentText: '',
				dyId: '',
				answerList: [],
				examinationName: '',
				examinationId: '',
				looks: true,
				check: false,
				show: true,
				keyword: '',
				ispro: '1',
				list: ['进行中', '已完成'],
				current: 0,
				itemList: itemList,
				itemKeys: {
					titles: '',
					sel: '',
					steawwer: '',
				},
			}
    },
		onLoad(option) {
			if (option.check) {
				this.check = true
				this.getdataBuId(option.id)
				this.dyId = option.id
			} else {
				this.getApplyByIdFun(option.id)
			}
			if (option.looks) {
				this.looks = false
			}
			
		},
		onShow() {
			if (store.getters.answerList) {
				this.answerList = store.getters.answerList
			}
			if (store.getters.setdatesdy) {
				this.itemKeys = JSON.parse(JSON.stringify(store.getters.setdatesdy))
				 store.dispatch('sethtstady', '')
			}
		},
		methods: {
			listpaperFun(paperId) {
				listpaper({
					id: this.dyId,
					paperId,
				}).then((res) => {
					this.answerList = res.rows
				})
			},
			getdataBuId(id) {
				researcBuId(id).then((res) => {
					this.examinationId = res.data.examinationId
					this.examinationName = res.data.examinationName
					this.listpaperFun(res.data.examinationId)
					timed_task_type().then((item) => {
						this.itemKeys = {
							...res.data,
							photoList: [],
							typeName: item.data.filter(items => items.dictValue == res.data.type)[0].dictLabel,
							filterTimeSta: res.data.hallStartTime,
							filterTimeEnd: res.data.hallEndTime,
						}
					})
				})
			},
			setTaskVisitFun() {
				(this.check?staskResearch : setTaskResearch)({
					...this.datasAll,
					paperAnswerList: this.answerList,
					status: 2,
				}).then((res) => {
					uni.showToast({
					  title: this.check? '修改成功' : '保存并提交成功',
					  icon: "none",
					  duration: 2000,
						success: () => {
							setTimeout(() => {
								uni.navigateBack({
									delta: 1,
								})
							}, 500)
						}
					})
					
				})
			},
			confirmFunXz() {
				this.setTaskVisitFun()
				this.showTimefilter = false
			},
			setdataFun(val) {
				this.datasAll = val
				if (!this.answerList.length) {
					uni.showToast({
					  title: '请先提交问卷',
					  icon: "none",
					  duration: 2000,
					})
					return
				}
				verifyFunDy(val).then((vido) => {
					if (vido.data && vido.data.isPass === false) {
						this.showTimefilter = true
						this.contentText = vido.data.wrongContent
						return
					}
					if (vido.data && vido.data.isPassMust === false) {
						
						uni.showToast({
						  title: vido.data.wrongContentMust,
						  icon: "none",
						  duration: 2500,
						})
						
						return
					}
					this.setTaskVisitFun()
				})
			},
			getApplyByIdFun(id) {
				getApplyById(id).then((res) => {
					this.examinationId = res.data.examinationId
					this.examinationName = res.data.examinationName
					let {type, drugName, province, hospital, hospitalList, hallName, drugOwner, researchObject } = res.data
					timed_task_type().then((item) => {
						this.itemKeys = {
							typeName: '',
							name: res.data.taskName,
							status: 1,
							photoList: [],
							hospitalList,
							typeName: item.data.filter(items => items.dictValue == res.data.type)[0].dictLabel,
							type,
							drugName,
							province,
							hospital,
							departmentName: '',
							departmentId: '',
							doctorName: '',
							doctorId: '',
							target: '',
							startTime: '',
							endTime: '',
							resourceName: '',
							resourceId: '',
							urlFil: '',
							doctorIdea: '',
							applyId: res.data.id,
							hallName,
							drugOwner,
							researchObject,
							patientCity: '',
							filterTimeSta: res.data.startTime,
							filterTimeEnd: res.data.endTime,
							year: res.data.year,
							month: res.data.month,
						}
						hospitalById(res.data.hospitalList[0].hospitalId).then((itemHo) => {
							this.itemKeys.patientCity = itemHo.data.province + itemHo.data.city +itemHo.data.area
						})
					})
				})
			},
			confirm() {
				this.$refs.getFrom.submit()
				// .then((res) => {
				// 	console.log(res)
				// })
			},
			proFun(val) {
				this.ispro = val
			},
			sectionChange(index) {
				this.current = index;
			},
			quertionFun() {
				store.dispatch('sethtstady', this.$refs.getFrom.setitemKeyList())
				if (this.check || this.answerList.length  == 0) {
					uni.navigateTo({
						url: "/pages/mytaskManagement/questionResearch/index?id=" + this.examinationId + '&dyId=' + this.dyId + '&looks=' + (this.looks? 1 : 2)
					})
				} else {
					uni.showToast({
					  title: '您已答完问卷',
					  icon: "none",
					  duration: 2000,
					})
				}
			},
		},
		mounted() {},
  }
</script>
<style lang="scss" scoped>
	.sea_pad {
		padding: 0 30rpx;
		box-sizing: border-box;
	}
	.container {
		background-color: #f7f8ff;
		// background-color: #fff;
		display: flex;
		flex-direction: column;
		height: 100vh;
		overflow: hidden;
	}
	.province {
		flex: 1;
		overflow: scroll;
		padding: 0 20rpx;
		margin-top: 20rpx;
		padding: 0 40rpx;
		box-sizing: border-box;
	}
	.button_str {
		width: 80%;
		margin: auto;
		text-align: center;
		border-radius: 100rpx;
		padding: 20rpx 10rpx;
		background-color: rgb(3, 121, 255);
		color: #fff;
		box-sizing: border-box;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
	}
	.researc_flex {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.button_bgcolor {
		background-color: orange;
		color: #fff;
		width: 100rpx;
		padding: 10rpx 0rpx;
		font-size: 26rpx;
		box-sizing: border-box;
		border-radius: 50rpx;
		text-align: center;
	}
	.img_box{
	  position: absolute;
	  top: 0;
	  bottom: 0;
	  left: 0;
	  right: 0;
	}
	.img_box image{
	  width: 100%;
	  height: 100%;
	}
</style>
